
/** 今年是否是传入的时间 */
export function isToday(time){
    return now().toDateString() === date(time).toDateString();
}

export function toDateStr(date){
    return parseTime(date,'{y}-{m}-{d}');
}

export function toTimeStr(date){
    return parseTime(date,'{h}:{i}:{s}');
}

export function nowDateStr(){
    return toDateStr(now())
}

export function nowTimeStr(){
    return toTimeStr(now())
}


/** 将秒数转为 00:00:00 */
export function secondFormat(second){
    if (second === undefined || second == null ||second <= 0){
        return "00:00:00";
    }
    let h = parseInt(second / 60 / 60 % 24);
    let m = parseInt(second / 60 % 60);
    let s = parseInt(second % 60);
    //补零 如果小于10 则在前边进行补零 如果大于10 则不需要补零
    h = h < 10 ? '0' + h : h;
    m = m < 10 ? '0' + m : m;
    s = s < 10 ? '0' + s : s;
    return `${h}:${m}:${s}`
}

/** 将时分秒转为秒数 */
export function timeToSecond(time){
    const times = time.split(':');
    if (times.length === 1){
        return Number(times[0]);
    }else if(times.length === 2){
        return Number(times[0]*3600) + Number(times[1]*60);
    }else{
        return Number(times[0]*3600) + Number(times[1]*60) + Number(times[2]);
    }
}


/** 判断目标时间是否在 指定时间之内 */
export function isRange(target,start,end){
    return before(start,target) && after(end,target);
}

/** 判断目标时间是否在 指定时间之前 */
export function before(target,time){
    return date(time) > date(target);
}


/** 判断目标时间是否在 指定时间之后 */
export function after(target,time){
    return date(time) < date(target);
}

/** 判断当前时间是否在 指定时间之内 */
export function isInNow(start,end){
    return isRange(now(),start,end);
}

/** 判断当前时间是否在 指定时间之前 */
export function beforeNow(target){
    return before(now(),target);
}


/** 判断当前时间是否在 指定时间之后 */
export function afterNow(target){
    return after(now(),target);
}

/**
 * 返回时间范围内的所有天
 */
export function getRangeDay(start,end){
    let date_all = [], i = 0;
    const startTime = date(start);
    const endTime = date(end);
    while((endTime.getTime()-startTime.getTime())>=0){
        const year = startTime.getFullYear();
        const month = startTime.getMonth() < 10 ? "0" + (startTime.getMonth() + 1) : startTime.getMonth() + 1;
        const day = startTime.getDate() < 10 ? "0" + startTime.getDate() : startTime.getDate();
        date_all[i]= `${year}-${month}-${day}`;
        startTime.setDate(startTime.getDate()+1);
        i++;
    }
    return date_all;
}

/** 当前时间 */
export function date(date){
    return new Date(date);
}

/** 当前时间 */
export function now(){
    return new Date();
}

/** 昨天 */
export function yesterday(){
    let yesterday = now();
    yesterday.setDate(yesterday.getDate()-1);
    yesterday.setTime(0)
    yesterday.setHours(0)
    yesterday.setSeconds(0);
    return yesterday;
}

/** 明天 */
export function tomorrow(){
    let tomorrow = now();
    tomorrow.setDate(tomorrow.getDate()+1);
    tomorrow.setTime(0)
    tomorrow.setHours(0)
    tomorrow.setSeconds(0);
    return tomorrow;
}

/**
 * 使用promise 类似于java 的sleep
 * console.log(1, new Date().getTime())     // 1 1631672405810
 * sleep(2000).then(() => {
 *     console.log(2, new Date().getTime()) // 2 1631672407810
 * })
  */
export function sleep(delay) {
    return new Promise(resolve => setTimeout(resolve, delay));
}

/**
 * 通用js方法封装处理
 * Copyright (c) 2019 ruoyi
 */

// 日期格式化
export function parseTime(time, pattern) {
    if (arguments.length === 0 || !time) {
        return null
    }
    const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}'
    let date
    if (typeof time === 'object') {
        date = time
    } else {
        if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
            time = parseInt(time)
        } else if (typeof time === 'string') {
            time = time.replace(new RegExp(/-/gm), '/').replace('T', ' ').replace(new RegExp(/\.[\d]{3}/gm), '');
        }
        if ((typeof time === 'number') && (time.toString().length === 10)) {
            time = time * 1000
        }
        date = new Date(time)
    }
    const formatObj = {
        y: date.getFullYear(),
        m: date.getMonth() + 1,
        d: date.getDate(),
        h: date.getHours(),
        i: date.getMinutes(),
        s: date.getSeconds(),
        a: date.getDay()
    }
    const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
        let value = formatObj[key]
        // Note: getDay() returns 0 on Sunday
        if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] }
        if (result.length > 0 && value < 10) {
            value = '0' + value
        }
        return value || 0
    })
    return time_str
}


